/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-03-11
 * Time: 14:24
 */
public class test3 {
    /**
     * LeetCode 134  加油站
     */

    public int canCompleteCircuit(int[] gas, int[] cost) {
        int n = gas.length;
        int sum = 0;
        int minSpare = Integer.MAX_VALUE;
        int minIndex = 0;
        for(int i=0;i<n;i++){
            sum += gas[i]-cost[i];
            if(sum<minSpare){
                minSpare = sum;
                minIndex = i;
            }
        }
        return sum<0?-1:(minIndex+1)%n;
    }
}
